package jp.co.showbiz.admin.report.action;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import jp.co.showbiz.admin.report.imp.ReportDetailDto;
import jp.co.showbiz.admin.report.imp.ReportDto;
import jp.co.showbiz.admin.report.imp.ReportServiceImpl;
import jp.co.showbiz.admin.talent.imp.TalentDto;
import jp.co.showbiz.common.base.BaseActionSupport;
import jp.co.showbiz.common.base.BaseException;
import jp.co.showbiz.common.entity.CommonMstBean;

/**
 * 
 * @author AIS
 *
 */
public class ReportAction extends BaseActionSupport {
	private static final long serialVersionUID = 2688908938721240256L;
    
	private boolean modifyFlg;
	private String disc;
	private String discValue;
	private List<TalentDto> talents;
	private List<ReportDto> reports;
	private List<CommonMstBean> movieKubun;
	private ReportDto rBean;
	private ReportDetailDto detail;
	protected HashMap<String, String> globeMap;
	
	public ReportAction() throws BaseException, IOException, SQLException {

		globeMap = new HashMap<String, String>();
		globeMap.put("globe.path.commonImg", getText("globe.path.commonImg"));
		globeMap.put("globe.path.root", getText("globe.path.root"));
		globeMap.put("golbe.path.tempDigestImgPath", getText("golbe.path.tempDigestImgPath"));
		globeMap.put("globe.path.base", getText("globe.path.base"));
		globeMap.put("globe.path.photo", getText("globe.path.photo"));
		globeMap.put("globe.path.movie", getText("globe.path.movie"));
		globeMap.put("globe.path.flash", getText("globe.path.flash"));
		globeMap.put("globe.path.voice", getText("globe.path.voice"));
		
		globeMap.put("file.vga.thumb", getText("file.vga.thumb"));
		globeMap.put("file.qvga.thumb", getText("file.qvga.thumb"));
		globeMap.put("file.vga.middle", getText("file.vga.middle"));
		globeMap.put("file.qvga.middle", getText("file.qvga.middle"));
		globeMap.put("file.vga.complete", getText("file.vga.complete"));
		globeMap.put("file.qvga.complete", getText("file.qvga.complete"));
		globeMap.put("file.vga.complete2", getText("file.vga.complete2"));
		globeMap.put("file.qvga.complete2", getText("file.qvga.complete2"));
		
		globeMap.put("file.name.photo", getText("file.name.photo"));
		globeMap.put("file.name.shortmovie", getText("file.name.shortmovie"));
		globeMap.put("file.name.videoclip", getText("file.name.videoclip"));
		globeMap.put("file.name.flashcalender", getText("file.name.flashcalender"));
		globeMap.put("file.name.flashwait", getText("file.name.flashwait"));
		globeMap.put("file.name.voice", getText("file.name.voice"));
		
		ReportServiceImpl service = new ReportServiceImpl(globeMap);
		List<CommonMstBean> discs = service.getKijiDiscValue();
		for (CommonMstBean bean : discs) {
			globeMap.put(bean.getValue(), bean.getName());
		}
	}
	
	private HashMap<String, Integer> pageMap;
    private int curPage;
    
	public HashMap<String, Integer> getPageMap() {
		return pageMap;
	}
	public void setPageMap(HashMap<String, Integer> pageMap) {
		this.pageMap = pageMap;
	}
	public int getCurPage() {
		return curPage;
	}
	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}
	public List<ReportDto> getReports() {
		return reports;
	}
	public void setReports(List<ReportDto> reports) {
		this.reports = reports;
	}
	public ReportDto getRBean() {
		return rBean;
	}
	public void setRBean(ReportDto bean) {
		rBean = bean;
	}
	public boolean isModifyFlg() {
		return modifyFlg;
	}
	public void setModifyFlg(boolean modifyFlg) {
		this.modifyFlg = modifyFlg;
	}
	public String getDiscValue() {
		return discValue;
	}
	public void setDiscValue(String discValue) {
		this.discValue = discValue;
	}
	public String getDisc() {
		return disc;
	}
	public void setDisc(String disc) {
		this.disc = disc;
	}
	public List<TalentDto> getTalents() {
		return talents;
	}
	public void setTalents(List<TalentDto> talents) {
		this.talents = talents;
	}
	public ReportDetailDto getDetail() {
		return detail;
	}
	public void setDetail(ReportDetailDto detail) {
		this.detail = detail;
	}
	public List<CommonMstBean> getMovieKubun() {
		return movieKubun;
	}
	public void setMovieKubun(List<CommonMstBean> movieKubun) {
		this.movieKubun = movieKubun;
	}
	@Override
	public void prepare() throws Exception {
		if (disc == null) disc = "";
		if (rBean == null) rBean = new ReportDto();
		if (detail == null) detail = new ReportDetailDto();
    	setTalentList();
    	setMovieDisc();
	}
	private void setDiscValue() {
		if (disc.equals("cover")) {
			discValue = globeMap.get("co");
		} else if (disc.equals("interview")) {
			discValue = globeMap.get("in");
		} else if (disc.equals("issue")) {
			discValue = globeMap.get("is");
		} else if (disc.equals("wideshow")) {
			discValue = globeMap.get("wi");
		}
	}
	private void setTalentList() throws Exception {
		ReportServiceImpl service = new ReportServiceImpl(globeMap);
		talents = service.getTalentList();
	}
	private void setMovieDisc() throws Exception {
		ReportServiceImpl service = new ReportServiceImpl(globeMap);
	    movieKubun = service.getMovieKubun();
	}
	@Override
    public String execute() throws Exception {
    	logger.debug("ReportAction.execute called.");
    	setDiscValue();
    	ReportServiceImpl service = new ReportServiceImpl(globeMap);
    	pageMap = service.createPageMap(disc, curPage);
    	reports = service.getReportList(disc, pageMap);
    	
    	return SUCCESS;
    }
    public String detail() throws Exception {
    	logger.debug("ReportAction.detail called.");
    	setDiscValue();
    	ReportServiceImpl service = new ReportServiceImpl(globeMap);
    	detail = service.getReportDetail(rBean);
    	detail.setDigests(service.getDigestList(rBean.getId()));
    	return SUCCESS;
    }
    public String create() throws Exception {
    	logger.debug("ReportAction.create called.");
    	setDiscValue();
    	modifyFlg = false;
    	return SUCCESS;
    }
    public String registe() throws Exception {
    	logger.debug("ReportAction.registe called.");
    	setDiscValue();
    	ReportServiceImpl service = new ReportServiceImpl(globeMap);
    	//Talent Set up
    	if (checkTalentId(rBean)){
	    	// 基本情報登録
	    	String kijiId = service.registeBagicReportInfo(rBean, disc);
	    	// アップロードファイルコピー
	    	HashMap<String, String> contentMap = service.moveReportResource(rBean, kijiId, disc);
	    	// コンテンツDB登録
	    	service.registeKijiContents(rBean, contentMap);
	    	modifyFlg = false;
    	}
    	
    	return SUCCESS;
    }
    private boolean checkTalentId(ReportDto bean) {
		// TODO Auto-generated method stub
    	if (bean.getTalent1().equals("")&& bean.getTalent2().equals("")&&
    			bean.getTalent3().equals("")){
			return false;
		}
		
    	if(!bean.getTalent1().equals("")){
    		bean.setTalent(bean.getTalent1());
    	}else if(!bean.getTalent2().equals("")){
    		bean.setTalent(bean.getTalent2());
    	}else{
    		bean.setTalent(bean.getTalent3());
    	}
    	
		String talId = bean.getTalent1();
		if (!talId.equals("")){
			if (talId.equals(bean.getTalent2())){
				bean.setTalent2("");
			}
			if (talId.equals(bean.getTalent3())){
				bean.setTalent3("");
			}
		}
		talId = bean.getTalent2();
		if (!talId.equals("")){
			if (talId.equals(bean.getTalent3())){
				bean.setTalent3("");
			}
		}
		return true;
	}
	public String modify() throws Exception {
    	logger.debug("ReportAction.modify called.");
    	setDiscValue();
    	ReportServiceImpl service = new ReportServiceImpl(globeMap);
    	rBean.setId(detail.getId());
    	rBean = service.getReportModifyDetail(rBean);
    	modifyFlg = true;
    	return SUCCESS;
    }
    public String update() throws Exception {
    	logger.debug("ReportAction.update called.");
	    setDiscValue();
	    ReportServiceImpl service = new ReportServiceImpl(globeMap);
	    if (checkTalentId(rBean)){
	    	service.updateReport(rBean);
	    }
    	return SUCCESS;
    }

}
